#!/bin/bash

######################################################################
#
#  Copyright (c) 2015 arakasi72 (https://github.com/arakasi72)
#
#  --> Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
#
######################################################################

PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/bin:/sbin
serverip=$(ip route get 8.8.8.8 | awk 'NR==1 {print $NF}')

if [ "$(id -u)" != "0" ]; then
  echo "Must be run as root, directly or with sudo"
  exit 1
fi

if [ $(dpkg-query -W -f='${Status}' webmin 2>/dev/null | grep -c "ok installed") -eq 0 ]; then
  echo "Installing Webmin"
  echo "deb http://download.webmin.com/download/repository sarge contrib" > /etc/apt/sources.list.d/webmin.list
  wget -qO - http://www.webmin.com/jcameron-key.asc | apt-key add -
  apt-get -qq update
  apt-get -qq install webmin
else
  echo 'Webmin is already installed'
fi

#changes to the webmin config file
if [ -z "$(grep ^referers_none= /etc/webmin/config)" ]; then
  echo "referers_none=1" >> /etc/webmin/config
else
  sed -i '/^referers_none=/ c\referers_none=1' /etc/webmin/config
fi

if [ -z "$(grep ^referers= /etc/webmin/config)" ]; then
  echo "referers=localhost" >> /etc/webmin/config
else
  sed -i '/^referers=/ c\referers=localhost' /etc/webmin/config
fi

if [ -z "$(grep ^webprefix= /etc/webmin/config)" ]; then
  echo "webprefix=/webmin" >> /etc/webmin/config
else
  sed -i '/^webprefix=/ c\webprefix=\/webmin' /etc/webmin/config
fi

if [ -z "$(grep ^webprefixnoredir= /etc/webmin/config)" ]; then
  echo "webprefixnoredir=1" >> /etc/webmin/config
else
  sed -i '/^webprefixnoredir=/ c\webprefixnoredir=1' /etc/webmin/config
fi

if [ -z "$(grep ^referer= /etc/webmin/config)" ]; then
  echo "referer=1" >> /etc/webmin/config
else
  sed -i '/^referer=/ c\referer=1' /etc/webmin/config
fi

if [ -z "$(grep ^ssl= /etc/webmin/miniserv.conf)" ]; then
  echo "ssl=0" >> /etc/webmin/miniserv.conf
else
  sed -i '/^ssl=/ c\ssl=0' /etc/webmin/miniserv.conf
fi

echo "Setting up Nginx Reverse Proxy"

#set up default proxy settings
echo 'proxy_redirect off;' > /etc/nginx/conf.d/proxy.conf
echo 'proxy_set_header Host $host;' >> /etc/nginx/conf.d/proxy.conf
echo 'proxy_set_header X-Real-IP $remote_addr;' >> /etc/nginx/conf.d/proxy.conf
echo 'proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> /etc/nginx/conf.d/proxy.conf
echo 'proxy_cache off;' >> /etc/nginx/conf.d/proxy.conf
echo 'proxy_buffering off;' >> /etc/nginx/conf.d/proxy.conf

#include webmin location in main nginx site file
sed -i '/webmin-loc/d' /etc/nginx/sites-available/default
sed -i '/^$/N;/^\n$/D'   /etc/nginx/sites-available/default
lineno=$(grep -Fn 'location ~ /\.ht' /etc/nginx/sites-available/default | cut -d : -f 1 | tail -1)
sed -i $lineno"i\        include \/etc\/nginx\/sites-available\/webmin-loc;\n" /etc/nginx/sites-available/default

#write the webmin nginx location file
echo 'location /webmin/ {' > /etc/nginx/sites-available/webmin-loc
echo '       proxy_pass http://localhost:10000/;' >> /etc/nginx/sites-available/webmin-loc
echo '       proxy_redirect http://$host:10000/ /webmin/;' >> /etc/nginx/sites-available/webmin-loc
echo '}' >> /etc/nginx/sites-available/webmin-loc

#restart services
service webmin restart  >> /dev/null 2>&1
service nginx restart  >> /dev/null 2>&1
echo "Completed"
echo
echo "Webmin can be accessed at https://$serverip/webmin"
echo "login using root, or sudo user credentials"
